package com.interceptor;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;

/**
 * Created by Ming on 2016/9/18.
 */

public class Interceptor implements HandlerInterceptor {
private Log log = LogFactory.getLog(this.getClass());
    private static final HashSet<String> whiteUrls = new HashSet<String>();
//   白名单
    static{
    whiteUrls.add("/login/login.do") ;
    whiteUrls.add("/loginPage.do");
}
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        log.debug("进入拦截器");
        //拿到url
        String url = whiteUrls.iterator().next();
//判断是不是在白名单里面,在就直接通过请求
        if(whiteUrls.contains(url)){
            return true;
        }
        if (request.getSession().getAttribute("user")==null){
            return false;}
        return true;
    }

    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
